package com.blankj.utilcode.util.ui;

import android.app.Activity;
import android.util.Log;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Activity管理工具
 */
public class ActivityManagerUtils {
    private static final String TAG = "ActivityManagerUtils";
    private static ActivityManagerUtils manager;
    private List<Activity> activityList;

    private ActivityManagerUtils() {
        activityList = new ArrayList<>();
    }

    public static ActivityManagerUtils getIntence() {
        if (manager == null) {
            manager = new ActivityManagerUtils();
        }
        return manager;
    }

    /**
     * 添加Activity到管理工具
     */
    public void addActivity(Activity activity) {
        if (activityList != null) {
            activityList.add(activity);
        }
    }

    /**
     * 移除Activity
     */
    public void removeActivity(Activity activity) {
        if (activityList != null && activityList.contains(activity)) {
            activityList.remove(activity);
        }
    }

    public void removeActivity1(String str) {
        Iterator iterator = activityList.iterator();
        while (iterator.hasNext()) {
            Activity activity = (Activity) iterator.next();
            if (str.equals(activity.getClass().getSimpleName())) {
                activity.finish();
                iterator.remove();
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (Activity activity : activityList) {
            Log.d(TAG, "activity:" + activity);
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public Activity topActivity() {
        if (activityList != null && activityList.size() > 0) {
            return activityList.get(activityList.size() - 1);
        }
        return null;
    }
}
